package com.yc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yc.bean.OrderBean;
import com.yc.bean.OrderDetailBean;
import com.yc.mapper.OrderDetailMapper;
import com.yc.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zwei
 * @create 2024-10-08 15:33
 */
@Service
public class OrderBizImpl implements OrderBiz{
    private final OrderMapper orderMapper;
    private final OrderDetailMapper orderDetailMapper;
    @Autowired
    public OrderBizImpl(OrderMapper orderMapper, OrderDetailMapper orderDetailMapper) {
        this.orderMapper = orderMapper;
        this.orderDetailMapper = orderDetailMapper;
    }
    @Override
    public int setOrderList(int userid, String order_code, Integer table_id, double total_Price) {
        OrderBean orderBean = new OrderBean();
        orderBean.setUserid(userid);
        orderBean.setOrder_code(order_code);
        orderBean.setTable_id(table_id);
        orderBean.setTotal_price(total_Price);
        orderBean.setOrder_date(LocalDateTime.now());
        orderBean.setUpdate_date(LocalDateTime.now());
        orderBean.setDisabled(0);
        return orderMapper.insert(orderBean);
    }

    @Override
    public List<OrderBean> find(int userid) {
        QueryWrapper<OrderBean>qw=new QueryWrapper<>();
        qw.select("id","order_code","table_id","total_price","order_date","order_status")
                .eq("disabled",0)
                .eq("userid",userid)
                .orderByDesc("case when order_status = 1 then 0 else 1 end", "order_date");
        List<OrderBean>list=orderMapper.selectList(qw);
        return list;
    }

    @Override
    public Boolean update(String order_code) {
        UpdateWrapper<OrderBean> qw=new UpdateWrapper<>();
        qw.set("order_status",1)
                .set("pay_date",LocalDateTime.now())
                .eq("order_code",order_code);
        int i=orderMapper.update(null,qw);
        return i>0;
    }

    @Override
    public Boolean delete(int id) {
        QueryWrapper<OrderDetailBean> qw=new QueryWrapper<>();
        qw.eq("orderid",id);
        orderDetailMapper.delete(qw);
        QueryWrapper<OrderBean> qw1=new QueryWrapper<>();
        qw1.eq("id",id);
        int i=orderMapper.delete(qw1);
        return i>0;
    }

    @Override
    public Boolean updatePrice(String order_code) {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("order_code",order_code);
        // 先查询订单的总金额
        OrderBean orderBean=orderMapper.selectOne(qw);
        double total_price=orderBean.getTotal_price()-10;
        UpdateWrapper<OrderBean> qw1=new UpdateWrapper<>();
        // 减10元
        qw1.set("total_price",total_price)
             .eq("order_code",order_code);
        int i=orderMapper.update(null,qw1);
        return i>0;
    }
}
